package com.test.suanfa;

class Node {
    int data;
    Node next;
 
    Node(int d) {
        data = d;
        next = null;
    }
}
 
public class LinkedList {
    Node head;
 
    public LinkedList() {
        head = null;
    }
 
    public void append(int newData) {
        Node newNode = new Node(newData);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }
 
    public void display() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }

    public void delete(int deleteValue){
        Node current = head;
        while (current != null) {
            if(current.data==deleteValue){
                if(current.next!=null) {
                    Node node = current.next.next;
                    current.next = node;
                    break;
                }else {
                    current.next = null;
                    break;
                }
            }
            current = current.next;
        }
    }
 
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
 
        list.append(1);
        list.append(2);
        list.append(3);
        list.append(4);

        list.delete(4);

        list.display(); // Output: 1 2 3
    }
}